﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace Luna
{
	public static class FileExtension 
	{
		public static string readWebFile(string uri)
		{
			//string result = string.Empty;
			//HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
			//request.Proxy = WebRequest.DefaultWebProxy;
			//request.Timeout = 10 * 60 * 1000;
			//using (var res = request.GetResponse())
			//using (var s = new StreamReader(res.GetResponseStream()))
			//    result = s.ReadToEnd();
			//return result;
			WebClient wc = new WebClient();
			
			wc.Proxy = WebRequest.DefaultWebProxy;
			return wc.DownloadString(uri);
		}

		public static string readWebFile(string uri, string cacheFile)
		{


			if (string.IsNullOrEmpty(cacheFile))
				return readWebFile(uri);
			else {
				if (!File.Exists(cacheFile)) {
					string data = readWebFile(uri);
					File.WriteAllText(cacheFile, data);
					return data;
				}
				else return File.ReadAllText(cacheFile);
			}
		}

		public static string getFileName(this string filename) {
			return Path.GetFileName(filename);
		}

		public static string getFileExt(this string filename) {
			return Path.GetExtension(filename);
		}

		public static string getFileNameNoExt(this string filename) {
			return Path.GetFileNameWithoutExtension(filename);
		}

		public static string combinePath(this string path1, string path2) {
			return Path.Combine(path1, path2);
		}

		public static bool fileExists(this string filename) {
			return System.IO.File.Exists(filename);
		}

		public static void deleteFile(this string filename) {
			File.Delete(filename);
		}
	}
}
